package com.android.launcher3.views;

import android.content.Context;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.android.launcher3.AppInfo;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.widget.DateWidget;
import com.android.launcher3.widget.FirstScreenLayout1;
import com.android.launcher3.widget.FirstScreenLayout2;
import com.android.launcher3.widget.FirstScreenLayout3;

///负一屏
public class FirstScreen2 extends FrameLayout {

    private final static double SCALE = 3.27;

    private RelativeLayout container;
    private DateWidget dateWidget;

    FirstScreenType firstScreenType;
    SurfaceView mLayout1Surface;
    SurfaceView mLayout3Surface;
    FirstScreenLayout1 mLayout1;
    FirstScreenLayout2 mLayout2;
    FirstScreenLayout3 mLayout3;

    public FirstScreen2(@NonNull Context context) {
        super(context);
        init();
    }

    public FirstScreen2(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FirstScreen2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.first_screen2, this, true);
        container = findViewById(R.id.container);
        mLayout1 = findViewById(R.id.layout1);
        mLayout1Surface = mLayout1.findViewById(R.id.surface_widget);

        mLayout2 = findViewById(R.id.layout2);
        mLayout3 = findViewById(R.id.layout3);
        mLayout3Surface = mLayout3.findViewById(R.id.surface_widget_layout3);

        dateWidget = findViewById(R.id.date_widget);
        screenAdapter();
    }

    public void setFirstScreenType(FirstScreenType firstScreenType) {
        this.firstScreenType = firstScreenType;
        if (firstScreenType == FirstScreenType.layout1) {
            mLayout1.setVisibility(VISIBLE);
            mLayout1Surface.setVisibility(VISIBLE);

            mLayout2.setVisibility(INVISIBLE);
            mLayout3.setVisibility(INVISIBLE);
            mLayout3Surface.setVisibility(GONE);
        } else if (firstScreenType == FirstScreenType.layout2) {
            mLayout1.setVisibility(INVISIBLE);
            mLayout1Surface.setVisibility(GONE);

            mLayout2.setVisibility(VISIBLE);

            mLayout3.setVisibility(INVISIBLE);
            mLayout3Surface.setVisibility(GONE);
        } else {
            mLayout1.setVisibility(INVISIBLE);
            mLayout1Surface.setVisibility(GONE);
            mLayout2.setVisibility(INVISIBLE);
            mLayout3.setVisibility(VISIBLE);
            mLayout3Surface.setVisibility(VISIBLE);
        }

    }


    public void bindWidget() {

        mLayout2.bindWidget();
        mLayout3.bindWidget();
    }

    public void notifyMapWidgetChanged(String pkgName,String className) {

        mLayout1.notifyMapWidgetChanged(pkgName,className);
        mLayout2.notifyMapWidgetChanged(pkgName,className);
        mLayout3.notifyMapWidgetChanged(pkgName,className);
    }

    public enum FirstScreenType {
        layout1, layout2, layout3
    }

    void screenAdapter() {
        dateWidget.useAutoType();
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

        if (Utilities.isLongStripScreen(getContext())) {

            container.setPadding(Utilities.pxFromDp(15, displayMetrics), 0, Utilities.pxFromDp(15, displayMetrics), Utilities.pxFromDp(95, displayMetrics));

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateWidget.getLayoutParams();
            layoutParams.setMargins(layoutParams.leftMargin, Utilities.pxFromDp(5, displayMetrics), layoutParams.rightMargin, layoutParams.bottomMargin);
            dateWidget.setLayoutParams(layoutParams);
        } else if (Utilities.isSquareScreen(getContext()) || getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateWidget.getLayoutParams();
            layoutParams.setMargins(layoutParams.leftMargin, Utilities.pxFromDp(5, displayMetrics), layoutParams.rightMargin, layoutParams.bottomMargin);
            dateWidget.setLayoutParams(layoutParams);
        }

    }
}

